package com.venn.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * calculate current date - between "2016-07-18 16:38:00" to current date
 * get the year, month, day, hour, min, second
 * @author venn
 *
 */
public class CalculateTime {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		// transfer string to date
		Date startDate = null;
		try {
			startDate = transferStringToDate(startTimeStr,sdf);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// get current date
		Date currentDate = new Date();

		// get year between
		int year = getYear(currentDate,startDate);

		// get month  between
		// month have 28 to 31 day, so i will get day, use the second
		int month = getMonth(year,currentDate,startDate);

		// get second between
		long second = (currentDate.getTime() - startDate.getTime())/1000;

		// get second between
		long minute = second/60;

		// get hour between
		long hour = minute/60;

		// get day 
		long day = hour/24;

		System.out.println("year = " + year +" year");
		System.out.println("month = " + month +" month");
		System.out.println("day = " + day +" day");
		System.out.println("hour = " + hour +" hour");
		System.out.println("minute = " + minute +" min");
		System.out.println("second = " + second +" sec");


		System.out.println(startDate);

	}

	/**
	 * get the day between startDate to currentDate 
	 * @param currentDate
	 * @param startDate
	 * @return
	 */
	private static int getDay(int year, Date currentDate, Date startDate) {
		int currentMonth = currentDate.getMonth();
		int startMonth = startDate.getMonth();
		if (year > 0) {
			currentMonth += 12*year;
		}
		return currentMonth - startMonth;
	}
	/**
	 * get the month between startDate to currentDate 
	 * @param currentDate
	 * @param startDate
	 * @return
	 */
	private static int getMonth(int year, Date currentDate, Date startDate) {
		int currentMonth = currentDate.getMonth();
		int startMonth = startDate.getMonth();
		if (year > 0) {
			currentMonth += 12*year;
		}
		return currentMonth - startMonth;
	}

	/**
	 * get the year between startDate to currentDate 
	 * @param currentDate
	 * @param startDate
	 * @return
	 */
	private static int getYear(Date currentDate, Date startDate) {
		return currentDate.getYear() - startDate.getYear();
	}

	/**
	 * transfer string to date
	 * formate : yyyy-MM-dd hh:mm:ss
	 * @param starttimestr2
	 * @return
	 * @throws ParseException
	 */
	private static Date transferStringToDate(String startTime,SimpleDateFormat sdf) throws ParseException {

		return sdf.parse(startTime);
	}

	private static final String startTimeStr = "2016-07-18 16:38:00";
}
